package com.guodi.map.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guodi.common.constant.StatusCode;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.tool.api.R;
import com.guodi.map.entity.MapCimSkylineViewport;
import com.guodi.map.mapper.MapCimSkylineViewportMapper;
import com.guodi.map.service.IMapCimSkylineViewportService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 服务实现类
 *
 * @author:廖龙威
 * @since:2021-03-23
 */
@Service
public class MapCimSkylineViewportServiceImpl extends ServiceImpl<MapCimSkylineViewportMapper, MapCimSkylineViewport> implements IMapCimSkylineViewportService {

    /**
     * 新增
     *
     * @param skylineViewport 实体对象
     * @param userId 用户Id
     * @return 成功则返回"新增成功"
     */
    @Override
    public R saveSkylineViewport(MapCimSkylineViewport skylineViewport, Long userId) {
        LambdaQueryWrapper<MapCimSkylineViewport> wrapper = new QueryWrapper().lambda();
        wrapper.eq(MapCimSkylineViewport::getName, skylineViewport.getName());
        if (this.count(wrapper) > 0) {
            return R.fail(StatusCode.REQUEST_CODE_OPERATION_FAILURE, "天际线名称已经存在");
        }
        skylineViewport.setId(UuidUtil.get32UUID());
        skylineViewport.setCreator(userId);
        skylineViewport.setCreateTime(new Date());
        this.save(skylineViewport);
        return R.success("新增成功");
    }

    /**
     * 修改
     *
     * @param skylineViewport 实体对象
     * @param userId 用户Id
     * @return 成功则返回"修改成功"
     */
    @Override
    public R updateSkylineViewport(MapCimSkylineViewport skylineViewport, Long userId) {
        LambdaQueryWrapper<MapCimSkylineViewport> wrapper = new QueryWrapper().lambda();
        wrapper.eq(MapCimSkylineViewport::getName, skylineViewport.getName());
        wrapper.notIn(MapCimSkylineViewport::getId, skylineViewport.getId());
        if (this.count(wrapper) > 0) {
            return R.fail(StatusCode.REQUEST_CODE_OPERATION_FAILURE, "天际线名称已经存在");
        }
        skylineViewport.setModifier(userId);
        skylineViewport.setModifyTime(new Date());
        this.updateById(skylineViewport);
        return R.success("修改成功");
    }
}
